"""
check if login or not
"""
#from novaclient.v1_1 import client
#nt = client.Client('adminUser', 'password', 'adminTenant', 'http://192.168.1.3:5000/v2.0')
import portalclient
nt=portalclient.PortalClient()
@auth.requires_login()
def management():
    """synchronize DB nova and portal"""
    flavorList=nt.flavors.list()
    for item in flavorList:
        rows= db(db.flavors.id==item.id).select()
        if rows:
            #print 'update lai thong tin'
            for row in rows:
                if row.flavor_name !=item.name:
                    db(db.flavors.id==item.id).update(flavor_name=item.name,flavor_cpu=item.vcpus,flavor_ram=item.ram,flavor_disk=item.disk)
        else:
            db.flavors.insert(id=item.id,flavor_name=item.name,flavor_cpu=item.vcpus,flavor_ram=item.ram,flavor_disk=item.disk)
    """select all flavors to view"""
    sysFlavor = db(db.flavors).select(orderby=db.flavors.id)

    #session.flash='if you want to create new flavor click next tab'
    return dict(sysFlavor=sysFlavor,flavorList=flavorList)
    
    #return dict()
def create():
    
        if request.vars: 
            iddb=db(db.flavors.id).select(orderby=db.flavors.id)
            length=len(iddb)-1
            newid=iddb[length].id+1
            try:
                result=nt.flavors.create(request.vars.flavor_name,request.vars.flavor_ram,
                              int(request.vars.flavor_cpu),int(request.vars.flavor_disk), 
                              newid,)     
                response.flash='form accepted:' 
                redirect(URL('management'))  
            except:
                response.flash='Your given flavor name have existence'  
          
        return locals()
        #redirect(URL('management'))
    
